package com.syh.jn.scm.service.impl.operation.project;

import cn.redscarf.commons.util.other.ExtAssert;
import cn.redscarf.commons.util.validator.ValidationUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.syh.core.ddd.infrastructure.repository.AggregateRepository;
import com.syh.core.ddd.service.AbstractServiceImpl;
import com.syh.jn.scm.domain.operation.entity.project.OperationProject;
import com.syh.jn.scm.domain.operation.repository.project.OperationProjectRepository;
import com.syh.jn.scm.dto.operation.project.OperationProjectMto;
import com.syh.jn.scm.dto.operation.project.OperationProjectSto;
import com.syh.jn.scm.dto.operation.project.OperationProjectVo;
import com.syh.jn.scm.service.api.operation.project.OperationProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;


/**
 * Date:2018/7/13
 * Author:Clown
 * 运营平台直营项目serviceImpl;
 */
@Service
@Transactional
public class OperationProjectServiceImpl extends AbstractServiceImpl<OperationProjectSto,OperationProjectMto,OperationProjectVo,OperationProject,Long> implements OperationProjectService {
  @Autowired
  private OperationProjectRepository operationProjectRepository;

  @Override
  protected AggregateRepository<OperationProject, Long> getAggregateRepository() { return operationProjectRepository; }

  /**
   * 保存对象
   * @param sto
   * @return
   */
  @Override
  public Long save(OperationProjectSto sto){
    OperationProject project = new OperationProject();
    project.create(sto);
    return  operationProjectRepository.add(project);
  }

  /**
   * 修改
   * @param mto
   */
  @Override
  public void update(OperationProjectMto mto){
    OperationProject project = this.judgeNull(mto.getId());
    project.modify(mto);
    operationProjectRepository.update(project);
  }

  /**
   * 删除
   * @param id
   */
  @Override
  public void delete(Long id){
    OperationProject project = this.judgeNull(id);
    project.destroy();
    operationProjectRepository.remove(project);
  }

  /**
   * 启用项目
   * @param id
   */
  @Override
  public void enabled(Long id){
    OperationProject project = this.judgeNull(id);
    project.enabled();
    operationProjectRepository.updateStatus(project);
  }

  /**
   * 停用项目
   * @param id
   */
  @Override
  public void disabled(Long id){
    OperationProject project = this.judgeNull(id);
    project.disabled();
    operationProjectRepository.updateStatus(project);
  }

  /**
   * 根据id获取运营项目并且判空
   * @param id
   * @return
   */
  private OperationProject judgeNull(Long id) {
    OperationProject project = operationProjectRepository.getById(id);
    ExtAssert.notNull(project, "对象不存在");
    return project;
  }
}
